package com.nengchuan.kbms.dfs.controller.system;


import com.nengchuan.kbms.dfs.entity.dto.DataTagAttrDTO;
import com.nengchuan.kbms.dfs.entity.dto.DataTagAttrListDTO;
import com.nengchuan.kbms.dfs.entity.dto.DataTagAttrSortSwapDTO;
import com.nengchuan.kbms.dfs.entity.dto.DataTagDTO;
import com.nengchuan.kbms.dfs.entity.po.DataTag;
import com.nengchuan.kbms.dfs.entity.po.DataTagAttr;
import com.nengchuan.kbms.dfs.entity.vo.DataTagAttrListVO;
import com.nengchuan.kbms.dfs.entity.vo.DataTagAttrVO;
import com.nengchuan.kbms.dfs.entity.vo.DataTagVO;
import com.nengchuan.kbms.dfs.service.DataTagAttrService;
import com.nengchuan.kbms.dfs.service.DataTagService;
import com.nengchuan.model.model.WrappedResult;
import com.nengchuan.util.MapperUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 标签（元数据） 前端控制器
 * </p>
 *
 * @author abe
 * @since 2020-04-03
 */
@RestController
@RequestMapping("/sys/tag")
@Api(tags = "知识库数据后台管理 > 系统管理 > 文件管理 > 元数据管理 【已完成】")
public class TagController {

    @Autowired
    private DataTagService dataTagService;

    @Autowired
    private DataTagAttrService dataTagAttrService;

    // ----------------- 元数据类型
    @PostMapping("/save")
    @ApiOperation(value = "元数据类型新增")
    public WrappedResult<Long> save(@RequestBody DataTagDTO dto) {
        return WrappedResult.success(dataTagService.saveTag(dto));
    }

    @PostMapping("/update")
    @ApiOperation(value = "元数据类型修改")
    public WrappedResult<Boolean> update(@RequestBody DataTagDTO dto) {
        return WrappedResult.success(dataTagService.updateTag(dto));
    }

    @PostMapping("/delete")
    @ApiOperation(value = "元数据类型删除（只支持单个删除）")
    public WrappedResult<Boolean> delete(@ApiParam(value = "元数据类型id", required = true) @RequestParam Long id) {
        return WrappedResult.success(dataTagService.removeTag(id));
    }

    @PostMapping("/list")
    @ApiOperation(value = "元数据类型列表")
    public WrappedResult<List<DataTagVO>> list() {
        List<DataTag> dataTagList = dataTagService.list();
        List<DataTagVO> voList = MapperUtils.mappingList(dataTagList, DataTagVO.class);
        return WrappedResult.success(voList);
    }

    // ----------------- 元数据属性
    @GetMapping("/attr/data")
    @ApiOperation(value = "元数据属性详情")
    public WrappedResult<DataTagAttrVO> attrData(@ApiParam(value = "元数据属性id", required = true) @RequestParam Long id) {
        return WrappedResult.success(dataTagAttrService.getAttr(id));
    }

    @PostMapping("/attr/save")
    @ApiOperation(value = "元数据属性新增")
    public WrappedResult<Long> attrSave(@RequestBody DataTagAttrDTO dto) {
        return WrappedResult.success(dataTagAttrService.saveAttr(dto));
    }

    @PostMapping("/attr/update")
    @ApiOperation(value = "元数据属性修改")
    public WrappedResult<Boolean> attrUpdate(@RequestBody DataTagAttrDTO dto) {
        return WrappedResult.success(dataTagAttrService.updateAttr(dto));
    }

    @PostMapping("/attr/delete")
    @ApiOperation(value = "元数据属性删除")
    public WrappedResult<Boolean> attrDelete(@ApiParam(value = "元数据属性id", required = true) @RequestBody Serializable id) {
        return WrappedResult.success(dataTagAttrService.deleteAttr(id));
    }

    @PostMapping("/attr/list")
    @ApiOperation(value = "元数据属性列表")
    public WrappedResult<List<DataTagAttrListVO>> attrList(@RequestBody DataTagAttrListDTO dto) {
        List<DataTagAttr> dataTagAttrList = dataTagAttrService.listAttr(dto);
        return WrappedResult.success(MapperUtils.mappingList(dataTagAttrList, DataTagAttrListVO.class));
    }

    @PostMapping("/attr/swap")
    @ApiOperation(value = "元数据属性排序交换")
    public WrappedResult<Boolean> attrSwap(@RequestBody DataTagAttrSortSwapDTO dto) {
        return WrappedResult.success(dataTagAttrService.swap(dto));
    }

}
